/**
 * 添加或者修改页面
 */
var TMonitoringinfoInfoDlg = {
    data: {
        companyId: "",
        drain: "",
        detectTime: "",
        monitoringFactor: "",
        monitorConcentration: "",
        emissionRate: "",
        pollutantEmissions: "",
        monitoringReport: "",
        emissionCalculationProcess: "",
        monitoringPlan: ""

    }
};



layui.use(['form', 'admin','laydate','upload',  'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var upload = layui.upload;

    //让当前iframe弹层高度适应
    //admin.iframeAuto();




    /*监听公司名称*/
    var ajax1 = new $ax(Feng.ctxPath + "/tBasicinfo/basicList");
    var companyList = ajax1.start();
    console.log(companyList);
    for (var i = 0 ; i < companyList.length ; i++){
        $("#companyId").append("<option value=\"" + companyList[i].id + "\">" + companyList[i].companyName + "</option>");
    }
    //重新渲染
    layui.form.render("select");



    // 渲染时间选择框
    laydate.render({
        elem: '#detectTime'
    });


    /**
     * 上传监测报告
     */
    var uploadInst = upload.render({
        elem: '#reportImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#reporturlImg').attr('src', res.data);
            $("#reporturlImg").after('<input type="hidden" name="monitoringReport" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#reporturlImg');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    /**
     * 上传排放量计算过程
     */
    var uploadInst = upload.render({
        elem: '#processImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $('#processImage').attr('src', res.data);
            $("#processImage").after('<input type="hidden" name="emissionCalculationProcess" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#processImage');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    /**
     * 上传监测计划
     */
    var uploadInst = upload.render({
        elem: '#planImageBtn'
        ,url: Feng.ctxPath+'/ali/uploadFile'
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                loading = layer.load(0, {
                    shade: false,
                    time: 10*1000
                })
            });
        }
        ,done: function(res){
            console.log(res);
            layer.close(loading);
            $("#planImage").attr("src", res.data)
            // $('#planImage').attr('src', );
               // alert(sb)
            $('#monitoringPlan').attr("value",res.data)
            // $('#monitoringPlan').attr("value", res.data);
            // $("#planImage").after('<input type="hidden" name="monitoringPlan" value="' + res.data + '" />');
            //上传成功
        }
        ,error: function(){
            //演示失败状态，并实现重传
            var demoText = $('#planImage');
            layer.close(loading);
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a '+'class="layui-btn layui-btn-xs demo-reload">重试</ a>');
            demoText.find('.demo-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });



    //监听有无组织

    form.on('select(isOrganization)', function (data) {
        console.log(data.elem); //得到select原始DOM对象
        console.log(data.value); //得到被选中的值
        console.log(data.othis); //得到美化后的DOM对象
        var  sb = document.getElementById("isOpen");

        console.log(sb); //得到select原始DOM对象
        if (data.value == 1) {
            sb.style.display = "none"

        }else {
            sb.style.display = "block"
        }
    });








    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/tMonitoringinfo/addItem", function (data) {
            if (data.code == 200) {
                Feng.success("添加成功！");
            }else{
                Feng.error("添加失败！" + data.message)
            }

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("添加失败！" + data.message)
        });


        ajax.set(data.field);

        data.field.emissionCalculationProcess = $('#processImage').attr('src');
        data.field.monitoringPlan = $('#monitoringImg').attr('src');
        data.field.monitoringReport = $('#monitoringReport').val();

        console.log($('#monitoringReport').val(),"+++++++++++++++++++++++++++++")
        ajax.start();

        return false;
    });


    // upload.render({ //上传图片
    //     elem: '#upload_img_icon1',
    //     url:Feng.ctxPath+'/ali/uploadFile',
    //     multiple: true, //是否允许多文件上传。设置 true即可开启。不支持ie8/9
    //     auto:true,//自动上传
    //     before: function(obj) {
    //         loading = layer.load(0, {
    //             shade: false,
    //             time: 10*1000
    //         })
    //     },
    //     done: function(res,index) {
    //         $('#layuiImg1').append('<div style="width:300px;height: 310px;float: left;margin-right: 5px" class="image-container"   id="smallPicContainer' + index + '">' +
    //             '<img id="showSmallPic' + index + '" name="layuiImg1" style="width:300px;height: 300px" src="' + res.data + '">' +
    //             '<div class="delete-css"><button  id="upload_smallPic_1' + index + '" style="width: 20px;height: 20px;"><i class="layui-icon ayui-icon-close">' +
    //             '</i></button>' +
    //             '</div>' +
    //             '</div>'
    //         );
    //         //删除某图片
    //         $("#upload_smallPic_1" + index).bind('click', function () {
    //             $("#smallPicContainer" + index).remove();
    //         });
    //         layer.close(loading);
    //         layer.msg('上传成功！')
    //
    //         debugger
    //          console.log(last_url + "******")
    //         if(last_url){
    //             $("#monitoringReport").val(last_url+","+res.data);
    //         }else {
    //             $("#monitoringReport").val( res.data);
    //         }
    //
    //         console.log( $("#monitoringReport").val() + "-------------")
    //     }
    //     ,error: function(){
    //         layer.msg('上传错误！');
    //     }
    // });


    /*多图上传*/
    var uploadInst  = upload.render({
        elem: '#btn_multiple_upload_img'
        ,url:  Feng.ctxPath+'/ali/uploadFile' //改成您自己的上传接口
        ,multiple: true
        ,before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#upload_image_list').append('<img id = "shabi" style="height: 66px;margin-left: 7px" src="'+ result +'   " title="单击删除" οnclick="delMultipleImgs(this)" class="layui-upload-img">')
            });
        }
        ,done: function(res){
            //上传完毕
            if (res.code== 0) {

                var last_url = $(".upload_image_url").val();
                var upload_image_url = "";
                if(last_url){

                    upload_image_url = last_url+","+res.data;
                    $("#monitoringReport").val(upload_image_url);

                }else {
                    upload_image_url = res.data;
                    $("#monitoringReport").val( upload_image_url);
                }
                $(".upload_image_url").val(upload_image_url);



            }
        }
    });


    /**
     * 多图清除按钮点击事件
     */
    $("#btn_image_clear").click(function () {
        $('#upload_image_list').html("");
        $(".upload_image_url").val('');
    });
    i
    function delMultipleImgs(this_img) {
        //获取下标
        var subscript = $("#upload_image_list img").index(this_img);
        multiple_images = $('.upload_image_url').val().split(",");
        //删除图片
        this_img.remove();
        //删除数组
        multiple_images.splice(subscript, 1);
        $('.upload_image_url').val(multiple_images);
    };



    $('#upload_image_list').on('click', $('shabi'), function(data){
        var subscript = $("#upload_image_list img").index(data.target);
        multiple_images = $('.upload_image_url').val().split(",");
         console.log(multiple_images,"////////////////////")
        //删除图片
        data.target.remove();
        //删除数组
        multiple_images.splice(subscript, 1);
        $('.upload_image_url').val(multiple_images);

    })



});